<?php

namespace App\Domain\ChannelContact\Controllers;

use App\Exceptions\BadRequestException;
use App\Http\Controllers\Controller;
use App\Domain\ChannelContact\Rules\CreateChannelContractPost;
use App\Domain\ChannelContact\Rules\UpdateChannelContractPost;
use App\Domain\ChannelContact\Services\ChannelContactService;
use App\Supports\Constant\PaginationConst;
use App\Supports\Formatter\ResponseFormatter;
use Dcat\Laravel\Database\Tests\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Symfony\Component\Console\Input\Input;
use Tymon\JWTAuth\Facades\JWTAuth;
use App\Domain\Account\Models\AccountModel;
use App\MinIo;

class ChannelContactController extends Controller
{

    //合同列表
    public function list(Request $request, ChannelContactService $channelContactService)
    {
        $this->validate($request, [
            'contact_id' => 'string|nullable',
            'start_time' => 'string|nullable',
            'end_time' => 'string|nullable',
            'status' => 'string|nullable',
            'channel_name' => 'string|nullable',
            'currentPage' => 'integer|min:1|nullable',
            'pageSize' => 'integer|min:1|nullable',
        ]);
        $param = [
            'contact_id' => $request['contact_id'],
            'start_time' => $request['start_time'],
            'end_time' => $request['end_time'],
            'status' => $request['status'],
            'channel_name' => $request['channel_name'],
            'currentPage' => $request->input('currentPage', 1),
            'pageSize' => $request->input('pageSize', PaginationConst::PAGE_SIZE),
        ];
        $channelContractPaginator = $channelContactService->list($param);
        $channelContractPaginator = new LengthAwarePaginator($channelContractPaginator->items(), $channelContractPaginator->total(), $channelContractPaginator->perPage(), $channelContractPaginator->currentPage());

        //格式化
        $keyMap = [
            'id' => 'id',
            'name' => 'name',
            'contact_id' => 'contact_id',
            'channel_name' => 'channel_name',
            'channel_id' => 'channel_id',
            'status' => 'status',
            'start_time' => 'start_time',
            'end_time' => 'end_time',
            'enclosure' => 'enclosure',
            'signatory' => 'signatory'
        ];
        return $this->success(ResponseFormatter::formatterPaginator($channelContractPaginator, $keyMap));
    }

    public function index()
    {
        $user = JWTAuth::parseToken()->touser();
        return $user['name'];
    }

    //创建合同
    public function create(CreateChannelContractPost $request, ChannelContactService $channelContactService)
    {
        $param = $request->validated();
        $res = $channelContactService->create($param);
        return $this->success($param);
    }

    //修改合同
    public function update(UpdateChannelContractPost $request, ChannelContactService $channelContactService)
    {
        $param = $request->validated();

        $res = $channelContactService->update($param);

        return $this->success($res);
    }

    //合同删除
    public function del(Request $request, ChannelContactService $channelContactService)
    {
        $res = $channelContactService->del($request['id']);
        return $this->success($res);
    }

    //合同详情
    public function info(Request $request, ChannelContactService $channelContactService)
    {
        $data = $channelContactService->info($request['id']);
        return $data;
    }

    //签订人列表
    public function signatorylist(AccountModel $accountModel)
    {
        $fir = $this->index();
        $data['list'] = $accountModel->where('status', '1')->get(['id', 'name']);
        $data['select'] = $fir;
        return $this->success($data);
    }


    //上传
    public function enclosure(Request $request, MinIo $minIo)
    {
        $file = $request['file'];
        foreach ($file as $v) {
            if ($v->isValid()) {
                $this->saveFiles_2($v);
            }
        }
        $data = $minIo->requestUpload($request, "file");
        return $this->success($data);
    }

    //下载
    public function getenclosure(Request $request, MinIo $minIo, ChannelContactService $channelContactService)
    {
        $re = $channelContactService->findByContactId($request['contact_id']);
        $pathName = explode(',', $re['enclosure']);
        foreach ($pathName as $v) {
            $data[] = $minIo->getUrl($v);
        }
        return $this->success($data);
    }

    public function saveFiles_2($file)
    {
        //文件后缀
        $fileTypes = array('pdf', 'docx', 'doc', 'jpg', 'png', 'jpeg');
        //获取文件类型后缀
        $extension = $file->extension();
        //是否是要求的文件
        $isInFileType = in_array($extension, $fileTypes);
        if ($isInFileType) {

        } else {
            throw new BadRequestException('类型错误');
        }
    }


}
